﻿using System;
using GDIT.Base.PageBaseHelper;
using GDIT.BLL;
using GDIT.Model;
using DevExpress.Xpo;

namespace GDIT.page.manage.user
{
    public partial class userOperate : System.Web.UI.Page
    {

        private BUsers user = null;
        private Mtb_Users userInfo = null;
        private BRoles role = null;
        private BDepartment depart = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            try 
	        {	        
		        if (string.IsNullOrEmpty(Session["UserName"].ToString()))
                {
                    PageBase.MessageBoxShowAndGo("检查到你还没有登录，请先登录，谢谢！", "~/page/manage/login.aspx", Page);
                    return;
                }
                if (!IsPostBack)
                {
                    BindData();
                    BindDepartment();
                    BindRoles();
                }
	        }
	        catch (Exception ex)
	        {
		        PageBase.MessageBoxShow("操作失败，原因：" + ex.Message.ToString(), Page);
	        }
        }
        
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_OnClick(object sender, EventArgs e)
        {
            try
            {
                if (CheckUserIsExist(txtName.Text.Trim()) == true)
                {
                    PageBase.MessageBoxShow("抱歉，用户名已存在！", Page);
                    return;
                }
                user = new BUsers();
                userInfo = new Mtb_Users();
                userInfo.UserName = txtName.Text.Trim();
                userInfo.UserPwd = txtPwd.Text.Trim();
                userInfo.Role.RoleName = ddlRole.SelectedValue;
                userInfo.Department.DepartmentName = ddlDepartment.SelectedValue;
                bool result = user.AddUser(userInfo);
                if (!result)
                {
                    BindData();//数据表绑定
                    PageBase.MessageBoxShow("抱歉，删除失败！", Page);
                }
                else
                {
                    BindData();//数据表绑定
                    PageBase.MessageBoxShow("恭喜, 删除成功！", Page);
                }
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作失败，原因：" + ex.Message.ToString(), Page);
            }
        }

        private bool CheckUserIsExist(string userName)
        {
            user = new BUsers();
            return user.IsExist(userName);
        }

        private void BindData()//绑定用户的数据
        {
            user = new BUsers();
            this.ASPxGridView1.KeyFieldName = "OID";
            this.ASPxGridView1.DataSource = user.GetAllUser();
            this.ASPxGridView1.DataBind();
        }

        private void BindRoles()//绑定角色数据
        {
            depart = new BDepartment();
            XPCollection xp = depart.SelectAllDepartment();
            foreach(Mtb_Department departmtnt in xp)
            {
                ddlDepartment.Items.Add(departmtnt.DepartmentName);
            }
        }

        private void BindDepartment()//绑定部门数据
        {
            role = new BRoles();
            XPCollection xp = role.SelectRoles();
            foreach (Mtb_Roles roles in xp)
            {
                ddlRole.Items.Add(roles.RoleName);
            }
        }

        protected void ASPxGridView1_PageIndexChanged(object sender, EventArgs e)
        {
            BindData();//只需重新绑定数据即可实现上下翻页
        }

        protected void ASPxGridView1_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
        {
            e.Cancel = true;//否则，只有刷新页面才能看到删除后的结果
            try
            {
                int id = Convert.ToInt32(e.Keys[0]);//获取ID
                user = new BUsers();
                bool result = user.DeleteUser(id);//从数据库删除记录
                if (!result)
                {
                    BindData();//数据表绑定
                    PageBase.MessageBoxShow("抱歉，删除失败！", Page);
                }
                else
                {
                    BindData();//数据表绑定
                    PageBase.MessageBoxShow("恭喜, 删除成功！", Page);
                }
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作失败，原因：" + ex.Message.ToString(), Page);
            }
        }
    }
}